package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.UUID;

@Slf4j
@Component
public class RedisTokenCleaner {
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String WHITE_LIST_KEY_PREFIX = "jwt:whitelist:";

    @EventListener(ApplicationReadyEvent.class)
    public void clearOldTokenWhiteList() {
        Set<String> keys = redisTemplate.keys(WHITE_LIST_KEY_PREFIX + "*");
        if (keys != null && !keys.isEmpty()) {
//            redisTemplate.delete(keys);
            log.info("系统启动完成，清除旧 token 白名单 {} 条", keys.size());
        } else {
            log.info("系统启动完成，未发现旧 token 白名单，无需清理");
        }
        // 2. 生成新的 startup-id 并存入 Redis（不过期）
        String startupId = UUID.randomUUID().toString();
//        redisTemplate.opsForValue().set("jwt:startup-id", startupId);
        log.info("新的启动ID为：{}", startupId);
    }
}
